var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    page:1,
    listData:[],
    orderStatus:0,
    show:false,
    len:1,
    money:0,
    bgcolor: home.getKey('bgcolor'),
    timer:'',//定时器
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#'+home.getKey('bgcolor'),
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    if(options['balance']>0){
      wx.setStorageSync('isback',2);
      home.setKey('balance',options['balance']);
      that.setData({
        money:options['balance'],
        active: options.active
      })
    }else{
      wx.setStorageSync('isback',1);
      that.setData({
        money:home.getKey('balance'),
        active: options.active
      })
    }
  },

  //读取余额
  getmoney(){
    let that = this;
    wx.request({
      url: url + 'app/getUserInfo',
      data:{
        storeId:home.getKey('storeId'),
        phone:home.getKey('phone'),
        xcxOpenId:home.getKey('openId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          if (res.data.result.balance == null) {
            res.data.result.balance = 0;
          }
          that.setData({
            balance:res.data.result.balance,
          });
          home.setKey('balance',res.data.result.balance);
        }else{
          home.setKey('balance',0);
          that.setData({
            btnState: false
          });
        }
      },
      fail: function (res) {
        home.setKey('balance',0);
        //home.wxMessage('请先登录','none');
        // wx.showToast({
        //   title: '请重新登录',
        //   icon: 'none'
        // });
        that.setData({
          btnState: false
        });
      }
    })
  },

  onChange(e) {
    console.log(e);
    this.setData({
      orderStatus:e.detail.index,
      active:e.detail.index,
      page:1,
      listData:[]
    });
    this.getBonusList();
  },

  //查询余额明细列表
  getBonusList(){
    let that = this;
    wx.request({
      //url: url + 'app/prebookList4Member',
      url: url + 'app/accountDetail',
      data:{
        storeId:home.getKey('storeId'),
        memberId:home.getKey('userId'),
        orderStatus:that.data.orderStatus
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          let listData = [];
          listData = res.data.result;
          let show = false;
          if (listData.length == 0) {
            show = true;
          }
          that.setData({listData:listData,show:show});
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none'
          })
        }
      }
    })
  },

  czClick() {
      wx.navigateTo({
          url: './chong',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if(wx.getStorageSync('isback') > 0){
      that.getmoney();
      that.setData({
        money:home.getKey('balance'),
        avatar:wx.getStorageSync('avatar'),
        username:wx.getStorageSync('nickName'),
      })
      that.getBonusList();
    }else{
      that.setData({
        timer:setInterval(function(){
          that.getmoney();
          that.setData({
            money:home.getKey('balance'),
            avatar:wx.getStorageSync('avatar'),
            username:wx.getStorageSync('nickName'),
          })
          that.getBonusList();
          clearInterval(that.data.timer);
        },500)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    if (this.data.len > 0) {
      this.setData({page:++page});
    }
    this.getBonusList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})